/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package ranab.server.ftp;

import java.io.File;
import java.net.Socket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Enumeration;

import ranab.server.BaseServer;
import ranab.server.ServerEngine;

/**
 * This is ftp server starting point. For each request it
 * instantiates a new <code>BaseFtpConnection</code> object.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class FtpServer extends BaseServer {                             
    
    // server name
    public final static String NAME = "FTP";
        
    // configuration object
    private FtpConfig mConfig;
    
    // pasv server socket
    private ServerSocket mPasvSocket;
    
    //////////////////////////////////////////////
    /**
     * Constructor.
     * @param configFile this ftp server configuration file. 
     */
    public FtpServer(File configFile) throws Exception {
        mConfig = new FtpConfig(configFile);
    }
    
    /**
     * Get server port.
     */
    public int getServerPort() {
        return mConfig.getServerPort();
    }
     
    /**
     * Get server bind address.
     */
    public InetAddress getServerAddress() {
        return mConfig.getSelfAddress();
    }
     
    /**
     * Get server name.
     */
    public String getServerName() {
        return NAME;
    }
     
    /**
     * Serves threaded request. 
     */
    public void serveRequest(Socket soc) {
        BaseFtpConnection ftpCon = new FtpConnection(mConfig, soc);
        new Thread(ftpCon).start();
    }
    
    /**
     * Stop all currently executing threads.
     */
    public void dispose() {
        if (mConfig != null) {
            mConfig.dispose();
            mConfig = null;
        }
    } 
    
    /**
     * Get the configuration object.
     */
    public FtpConfig getConfig() {
        return mConfig;
    }
        
    
    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    /**
     * Command line ftp server starting point.
     */
    public static void main(String args[]) {
        
        // input argument check
        String configFile = null;
        if(args.length == 0) {
            System.err.println("Usage: java ranab.server.ftp.FtpServer <config file>");
            System.err.println("Trying to use config file \"../apps/ftp/conf/ftpd.conf\"");
            configFile = "../apps/ftp/conf/ftpd.conf";
        }
        else {
            configFile = args[0];
        }
        
        try {
            FtpServer server = new FtpServer(new File(configFile));
            
            // print all property name/value pairs
            FtpConfig config = server.getConfig();
            Enumeration propNames = config.propertyNames();
            while(propNames.hasMoreElements()) {
                 String key = (String)propNames.nextElement();
                 System.out.println(key + " = " + config.getProperty(key));
            }

            final ServerEngine engine = new ServerEngine(server);
            
//            // add shutdown hook - since JDK1.3
//            Runnable shutdownHook = new Runnable() {
//                public void run() {
//                    System.out.println("Stopping server...");
//                    engine.stopServer();
//                }    
//            };
//            Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
            engine.startServer();
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }    
    }
}
